package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.cucc.service.attr.handler.ServiceTimeAttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @Description 雁飞5G 智连专线产品型号属性联动 产品服务期限
 * @Author deng.gang
 * @Date 2021/12/10 11:49 上午
 */
@Service
@AttrHandler(ServiceTimeAttrHandler.class)
public class ServiceTimeAttrFor5GLinkHandler extends AbstractAttrLinkageHandler<AttrData> {

    @Autowired
    IQuerySceneInstService querySceneInstService;

    @Autowired
    IOperateSceneInstService operateSceneInstService;

    private static final String SERVICE_PERIOD_FIELD_NAME = "200004106";

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        KiteOfferInst giftOfferInst = querySceneInstService.getGiftOfferInst(data.getSceneInstId());
        if (Objects.nonNull(giftOfferInst)) {
            List<KiteProdInst> prodInstList = querySceneInstService.listProdInstsFromGiftOfferInst(data.getCustId(), giftOfferInst.getOfferInstId());
            prodInstList.stream().forEach(kiteProdInst -> {
                KiteAttr periodAttr = kiteProdInst.getAttr(SERVICE_PERIOD_FIELD_NAME);
                if (Objects.nonNull(periodAttr)) {
                    periodAttr.setAttrId(attr.getAttrId());
                    periodAttr.setAttrValues(attr.getAttrValues());
                    periodAttr.setValue(attr.getValue());
                    periodAttr.setValueDesc(attr.getValueDesc());
                    operateSceneInstService.setAttr(data.getSceneInstId(), kiteProdInst.getInstType(), kiteProdInst.getInstId(), periodAttr);
                }
            });
        }
    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }
}
